package com.smartcruise.main.contactsFragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.smartcruise.R;
import com.smartcruise.data.Contacts;
import com.smartcruise.util.ToastUtils;
import com.smartcruise.util.customView.CircleImage;
import com.smartcruise.util.sortLetter.SortModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by FengChaoQun
 * on 2018/1/20 0020
 */

public class ContactsAdapter extends ArrayAdapter<SortModel<Contacts>> {

    public ContactsAdapter(@NonNull Context context, @NonNull List<SortModel<Contacts>> objects) {
        super(context, R.layout.item_contact, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.item_contact, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SortModel<Contacts> sortModel = getItem(position);
        final Contacts contacts = sortModel.getData();
        viewHolder.name.setText(contacts.getUserName());
        viewHolder.phone.setText(contacts.getPhoneNumber());
        viewHolder.departmentName.setText("这里是部门信息");
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final NormalDialog dialog = new NormalDialog(getContext());
                dialog.content("呼叫" + contacts.getUserName() + "?")
                        .btnText("取消", "呼叫")
                        .setOnBtnClickL(new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                            }
                        }, new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contacts.getPhoneNumber()));
                                if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                    ToastUtils.showShort(getContext(), "没有授权拨打电话，请在设置界面授权");
                                    return;
                                }
                                getContext().startActivity(intent);
                            }
                        });
                dialog.show();
            }
        });

        if (position == 0) {//第一个肯定要显示类型信息
            viewHolder.category.setVisibility(View.VISIBLE);
            if (sortModel.getRemark() != 0) {
                viewHolder.category.setText("紧急联系人");
            } else {
                viewHolder.category.setText(sortModel.getSortLetters());
            }
        } else {
            if (sortModel.isSameOrder(getItem(position - 1))) {
                viewHolder.category.setVisibility(View.GONE);
            } else {
                viewHolder.category.setVisibility(View.VISIBLE);
                viewHolder.category.setText(sortModel.getSortLetters());
            }
        }

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.category)
        TextView category;
        @BindView(R.id.headImage)
        CircleImage headImage;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.phone)
        TextView phone;
        @BindView(R.id.departmentName)
        TextView departmentName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
